package com.example.travelservlet.commentDynamic.controller;

import com.example.travelservlet.commentDynamic.dao.CommentDynamicMapper;
import com.example.travelservlet.commentDynamic.service.CommentDynamicService;
import com.example.travelservlet.commentEntertainment.service.CommentService;
import com.example.travelservlet.entity.Comment;
import com.example.travelservlet.entity.CommentDto;
import com.example.travelservlet.entity.DynamicComment;
import com.example.travelservlet.entity.DynamicCommentDto;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.user.domain.UserResult;
import com.example.travelservlet.utils.ResultUtil;
import com.example.travelservlet.utils.ResultUtilPlus1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;

/**
 * @author hxy
 * @date 2023/5/16 9:04
 */
@RestController
@RequestMapping("/commentDynamicController")
public class CommentDynamicController {

    @Autowired
    private CommentDynamicService commentDynamicService;

    @GetMapping("/insertDynamicComment/{dynamicId}/{content}/{userId}")
    public Integer insertDynamicComment(@PathVariable String dynamicId,@PathVariable String content,@PathVariable String userId){
        DynamicComment dynamicComment = new DynamicComment();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
        dateFormat.setTimeZone(timeZone);
        dynamicComment.setCommentId(UUID.randomUUID().toString());
        dynamicComment.setDynamicId(dynamicId);
        dynamicComment.setContent(content);
        dynamicComment.setCommentTime(dateFormat.format(new Date()));
        dynamicComment.setUserId(userId);
        return commentDynamicService.insertDynamicComment(dynamicComment);
    }

    @GetMapping("/findDynamicCommentByDynamicId/{dynamicId}")
    public Result<List<DynamicCommentDto>> findDynamicCommentByDynamicId(@PathVariable String dynamicId){
        List<DynamicCommentDto> dynamicCommentDtoList = commentDynamicService.findDynamicCommentByDynamicId(dynamicId);
        return ResultUtil.success(dynamicCommentDtoList);
    }

    @GetMapping("/findDynamicComments")
    public UserResult<List<DynamicComment>>findAllDynamicComments(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                                  @RequestParam(name = "size", required = false, defaultValue = "10") Integer size){
        List<DynamicComment> allDynamicComments = commentDynamicService.findAllDynamicComments();
        List<DynamicComment> dynamicComments = commentDynamicService.findAllDynamicComments1(page, size);
        return ResultUtilPlus1.success(dynamicComments,allDynamicComments.size());
    }
    @GetMapping("selectDynamicCommentsByKeyWord/{key}")
    public UserResult<List<DynamicComment>> findCommentByKeyWord(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                                 @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,@PathVariable String key){
        List<DynamicComment> comments=commentDynamicService.findCommentsByKeyWord(key);
        List<DynamicComment> commentList = commentDynamicService.findCommentsByKeyWord1(key,page,size);
        return ResultUtilPlus1.success(commentList,comments.size());
    }

    @GetMapping("deleteDynamicCommentById/{commentId}")
    public void deleteDynamicCommentById(@PathVariable String commentId){
        commentDynamicService.deleteDynamicCommentById(commentId);
    }
}
